package com.nondo.dean.auth.provider.impl;

import com.nondo.dean.auth.common.BusinessUtils;
import com.nondo.dean.auth.common.bean.PositionItem;
import com.nondo.dean.auth.domain.Position;
import com.nondo.dean.auth.provider.PositionManager;
import com.nondo.dean.auth.service.PositionService;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * project：dean-cloud
 *
 * @author linfeng @ nondo
 * @date 2018/4/23
 */
@Service
public class PositionManagerImpl implements PositionManager {

    @Autowired
    private PositionService positionService;

    @Override
    public View<String> requestSave(PositionItem item) {

        positionService.save(BusinessUtils.toPosition(item));

        return View.ofOk("岗位保存成功");
    }

    @Override
    public View<String> requestDelete(Long positionId) {

        positionService.delete(positionId);
        return View.ofOk("岗位删除成功");
    }

    @Override
    public View<String> requestMultiDelete(Long[] positionIds) {
        for (Long id : positionIds) {
            positionService.delete(id);
        }
        return View.ofOk("岗位删除成功");
    }

    @Override
    public View<Page<Position>> requestQuery(Query<PositionItem> item) {

        Position record = BusinessUtils.toPosition(item.getData());

        Query<Position> positionQuery = new Query<>();
        positionQuery.setSize(item.getSize());
        positionQuery.setPage(item.getPage());
        positionQuery.setData(record);

        List<Long> companyIds = new ArrayList<>();
        if (item.getData() != null) {
            companyIds = item.getData().getCompanyIds();
        }

        Page<Position> resultPage = positionService.findPositionAndCompanyIdsPage(positionQuery, companyIds);

        return View.ofOk(resultPage);
    }


}
